package com.dmz.etc.server.utils.etc;

import com.dmz.etc.server.config.AppConfig;
import com.dmz.etc.server.domain.MsgHeader;
import com.dmz.etc.server.utils.secure.CryptoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author lee
 */
@Slf4j
@Component
public class PackTool {

    @Resource
    private AppConfig appConfig;

    public byte[] getKeyBytes() {
        return appConfig.getKeyBytes();
    }

    public String getKey(){
        return appConfig.getKey();
    }

    public byte[] packSet(String reqBody, byte[] dynamicKey, MsgHeader ph)
            throws Exception {
        log.info("加密前的报文体:{}", reqBody);
        byte[] body = CryptoUtil.encrypt(reqBody, dynamicKey);
        log.info("加密后的报文体:{}", body);
        byte[] head = ph.headSet(reqBody);
        byte[] message = new byte[head.length + body.length];
        System.arraycopy(head, 0, message, 0, head.length);
        System.arraycopy(body, 0, message, head.length, body.length);
        log.info("组装后的报文:{}", message);
        return message;
    }
}
